<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class jQuickForm_Group_Repeatable_Link extends jQuickForm_Group_Repeatable {
    public function render1(HTML_QuickForm2_Renderer $renderer){
//        $renderer = new HTML_QuickForm2_Renderer_Default();
        $renderer->setTemplateForClass(strtolower(get_class($this)),'<div{attributes}>{content}</div>');
        $renderer->elementTemplatesForGroupClass[strtolower(get_class($this))]['html_quickform2_element'] = '
    <div class="repeatable" rel="{rel}" max="5">
        <table>
            <tr>
                <td>
                    {element}
                </td>
                <td>
                    <input class="rpt_up" value="&uarr;" type="button" />
                </td>
                <td>
                    <input class="rpt_down" value="&darr;" type="button" />
                </td>
                <td>
                    <input class="rpt_add" value="+" type="button" />
                </td>
                <td>
                    <input class="rpt_remove" value="-" type="button" />
                </td>
            </tr>
        </table>
    </div>
        ';
        foreach ($this->getElements() as $e) {
            $renderer->renderRepeatable($e, $this->repeatable_rel);
        }
        return $renderer;
    }

}
class HTML_QuickForm2_Renderer_Default_Group_Repeatable_Link extends HTML_QuickForm2_Renderer_Plugin{

    public function setRenderer(HTML_QuickForm2_Renderer $renderer)
    {
        parent::setRenderer($renderer);
    }

    public function renderRepeatable(HTML_QuickForm2_Node $element, $rel)
    {
//        var_dump($this->renderer->findTemplate($element));exit;
        $elTpl = $this->renderer->prepareTemplate($this->renderer->findTemplate($element), $element);
        $comment = $element->getComment();
        if($comment){
           $comment = '<div class="jqf_comment">'.$comment.'</div>';
        }
        $example = $element->getExample();
        $_example = '';
        if(is_array($example)){
//            split(",")
           $_example = '<div class="jqf_example"><strong>Например:</strong> <span class="ajax" value="'.$example['value'].'" rel="'.$element->getId().'">'.$example['text'].'</span></div>';
           jQuickForm::js()->addJsOnload('jQuery(".jqf_example span").click(function(){
    var id = jQuery(this).attr("rel");
    var val = jQuery(this).attr("value");

    if(val.indexOf(",") + 1) {
        jQuery("#"+id).val(val.split(","));
    } else {
        jQuery("#"+id).val(val);
    }
});','jqf_example');
        }
//        var_dump($elTpl);exit;
        $this->renderer->html[count($this->renderer->html) - 1][] = str_replace(array('{element}','{comment}', '{example}', '{id}'),'{rel}',
                                                            array($element, $comment,$_example, $element->getId(), $rel), $elTpl);
//        print str_replace(array('{element}','{comment}', '{example}', '{id}'),'{rel}',
//                                                            array($element, $comment,$_example, $element->getId(), $rel), $elTpl);exit;
    }
}
HTML_QuickForm2_Renderer::registerPlugin('default', 'HTML_QuickForm2_Renderer_Default_Group_Repeatable_Link');
?>